//
//  JMProfessionalSettingInfoViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/7.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMProfessionalSettingInfoViewController.h"
#import "JMAppNetworkClient.h"
#import "JMAttenButton.h"
#import "JMLoginViewController.h"

@interface JMProfessionalSettingInfoViewController ()<UIWebViewDelegate>



@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) JMAttenButton *attentionBtn;

@property (nonatomic, strong) UIWebView *webView;

//@property (nonatomic, strong) UIScrollView *backScrollView;


@end

@implementation JMProfessionalSettingInfoViewController


//- (UIScrollView *)backScrollView
//{
//    if (!_backScrollView) {
//        _backScrollView = [[UIScrollView alloc]init];
//        _backScrollView.backgroundColor = [UIColor clearColor];
//        _backScrollView.frame = CGRectMake(0, 0, UI_SCREEN_WITH, UI_SCREEN_HEIGHT);
//    }
//    return _backScrollView;
//}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UILabel* label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = UIColorFromRGB(0xeb6100);
        label.font = [UIFont systemFontOfSize:16.0f];
        label.frame = CGRectMake(UI_SCREEN_WITH *0.04125, 64 + 7, 180, UI_SCREEN_WITH *0.0625);
        _titleLabel = label;
    }
    return _titleLabel;
}

- (JMAttenButton *)attentionBtn
{
    if (!_attentionBtn) {
        JMAttenButton *button = [JMAttenButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(attentionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(UI_SCREEN_WITH - 65, 64 + 2, 50, 30);
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:UIColorFromRGB(0xa6a6a6) forState:UIControlStateNormal];
        _attentionBtn = button;
    }
    return _attentionBtn;
}

- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc]init];
        _webView.delegate=self;
        _webView.frame = CGRectMake(0, 64 + 35, UI_SCREEN_WITH, UI_SCREEN_HEIGHT - 64 - 35);
        _webView.scrollView.backgroundColor = [UIColor clearColor];
    }
    return _webView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.backBtn.hidden = YES;

    self.navigationItem.title =@"专业详情";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.attentionBtn];
    [self.view addSubview:self.webView];
    self.titleLabel.text =self.mode.titleName;
    if([self.mode.attention isEqualToString:@"1"]){
        self.attentionBtn.type = @"1";
        [self.attentionBtn setTitle:@"取消" forState:UIControlStateNormal];
        [self.attentionBtn setImage:[UIImage imageNamed:@"projectImage.bundle/guanzhu_image"] forState:UIControlStateNormal];
        [self.attentionBtn setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
    }else{
        self.attentionBtn.type = @"0";
        [self.attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
        [self.attentionBtn setImage:[UIImage imageNamed:@"projectImage.bundle/guanzhu_image_n"] forState:UIControlStateNormal];
    }
    
    [self.webView loadHTMLString:self.mode.jieshao baseURL:nil];
}

#pragma mark  guan zhu an niu dian ji
-(void)attentionButtonClick:(JMAttenButton *)button{

    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    
    
    if([button.type isEqualToString:@"1"]){
        [JMTools showAlertView:self.view];
        if(cf.user){
            NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
            [dict setObject:cf.user.userId forKey:@"user"];
            [dict setObject:cf.user.token forKey:@"token"];
            [dict setObject:[NSString stringWithFormat:@"%d",self.mode.professionalId] forKey:@"majorid"];
            [JMAppNetworkClient CancleProfessional_AttentionWithInfo :dict complete:^(id data, NSError *error) {
                [JMTools hiddenAlerView:self.view];

                if([[data objectForKey:@"status"] integerValue] == 1){
                    [JMTools showAlertView:self.view message:data[@"message"]];
                    [self.attentionBtn setTitleColor:UIColorFromRGB(0xa6a6a6) forState:UIControlStateNormal];
                    [self.attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
                    [self.attentionBtn setImage:[UIImage imageNamed:@"projectImage.bundle/guanzhu_image_n"] forState:UIControlStateNormal];
                    self.attentionBtn.type = @"0";
                }else{
                    [JMTools showAlertView:self.view message:data[@"message"]];
                    JMLoginViewController *vc =[JMLoginViewController new];
                    JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
                    [self presentViewController:nav animated:YES completion:^{
                        
                    }];
                }
            }];
        }
    }else{
        [JMTools showAlertView:self.view];
        if(cf.user){
            NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
            [dict setObject:cf.user.userId forKey:@"user"];
            [dict setObject:cf.user.token forKey:@"token"];
            [dict setObject:[NSString stringWithFormat:@"%d",self.mode.professionalId] forKey:@"majorid"];
            [JMAppNetworkClient AddProfessional_AttentionWithInfo:dict complete:^(id data, NSError *error) {
                [JMTools hiddenAlerView:self.view];
                if([[data objectForKey:@"status"] integerValue] == 1){
                    [JMTools showAlertView:self.view message:data[@"message"]];
                    [self.attentionBtn setTitleColor:UIColorFromRGB(0xeb6100) forState:UIControlStateNormal];
                    [self.attentionBtn setTitle:@"取消" forState:UIControlStateNormal];
                    [self.attentionBtn setImage:[UIImage imageNamed:@"projectImage.bundle/guanzhu_image"] forState:UIControlStateNormal];
                    self.attentionBtn.type = @"1";
                }else if([NSString stringWithFormat:@"%d",[data[@"status"] isEqualToString:@"-3"]]){
                    JMLoginViewController *vc =[JMLoginViewController new];
                    JMNavigationController *nav = [[JMNavigationController alloc]initWithRootViewController:vc];
                    [self presentViewController:nav animated:YES completion:^{
                        
                    }];
                }

                else{
                    [JMTools showAlertView:self.view message:data[@"message"]];
                }
            }];
        }
    }
}


-(void)webViewDidFinishLoad:(UIWebView *)webView{
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '80%'"];
//    //字体颜色
//    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= #676767"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
